import 'package:flutter/material.dart';
import 'package:tile/tile.dart';
import 'package:tile/view/painter/painter.dart';

class NumPainter extends TileNumPainter {
  final lastChessColor = Colors.red;

  @override
  paint(
    Canvas canvas, {
    Board? board,
    required int boardSize,
    required double Function(int num) toScreen,
    required double tileSize,
    required Rect rect,
    required bool isThumb,
    dynamic nodeExtraData,
  }) {
    final Board board = nodeExtraData['board']!;
    final TileNum tileNum = nodeExtraData['tileNum']!;

    if (tileNum == TileNum.none) return;

    if (board.isTrying) {
      var stepList = board.getTryingSteps();
      var paintList = List<ChessStep>.empty(growable: true);
      int count = stepList.length;
      for (int i = count; i >= 1; i--) {
        var step = stepList[i - 1];

        if (step.x == 0 || step.y == 0) continue;
        if (paintList.contains(step)) continue;
        paintList.add(step);

        Offset offset = Offset(toScreen(step.y), toScreen(step.x));
        Color color;
        if (i == count) {
          color = lastChessColor;
        } else {
          color = step.bw == 1 ? Colors.white : Colors.black;
        }
        _drawText(
          canvas: canvas,
          text: i.toString(),
          textColor: color,
          center: offset,
          fontSize: tileSize * 2 / 5,
        );
      }
    } else {
      var stepList = board.currNode.backStepListOrdered();
      var paintList = List<ChessStep>.empty(growable: true);
      int count = stepList.length;
      for (int i = count; i >= 1; i--) {
        var step = stepList[i - 1];

        if (step.x == 0 || step.y == 0) continue;
        if (paintList.contains(step)) continue;
        paintList.add(step);

        Offset offset = Offset(toScreen(step.y), toScreen(step.x));
        Color color;
        if (i == count) {
          color = lastChessColor;
        } else {
          color = step.bw == 1 ? Colors.white : Colors.black;
        }
        _drawText(
          canvas: canvas,
          text: i.toString(),
          textColor: color,
          center: offset,
          fontSize: tileSize * 2 / 5,
        );

        if (tileNum != TileNum.all) {
          break;
        }
      }
    }
  }

  void _drawText({
    required Canvas canvas,
    required String text,
    required Color textColor,
    required Offset center,
    required double fontSize,
    Rect? clearBoundRect,
  }) {
    if (clearBoundRect != null) _clearBound(canvas, clearBoundRect);

    TextSpan textSpan = TextSpan(
        style: TextStyle(
            color: textColor,
            fontSize: fontSize,
            fontWeight: FontWeight.bold,
            fontFamily: 'SiYuan'),
        text: text);
    TextPainter textPainter =
        TextPainter(text: textSpan, textDirection: TextDirection.ltr);
    textPainter.layout();
    textPainter.paint(
        canvas,
        Offset(center.dx - textPainter.width / 2,
            center.dy - textPainter.height / 2));
  }

  void _clearBound(Canvas canvas, Rect rect) {
    canvas.drawRect(
        rect,
        Paint()
          ..color = Colors.transparent
          ..blendMode = BlendMode.clear);
  }
}
